﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.Unity;
using ePlanet.ResourceRequest.Models;
using ePlanet.ResourceRequest.DataAccess;
using ePlanet.ResourceRequest.Services;
using ePlanet.SingleWell.Services;
using DreamBuilder.UI.UserControls;
using System.Data;
using DreamBuilder.Core.Container;
using ePlanet.SingleWell.Models;
using Wuqi.Webdiyer;

namespace Web.Views.ResourceRequestManage
{
    public partial class ResourceRequestRecordList : PageBase
    {

        #region 页面属性

        [Dependency]
        public IResourceRequestService m_IResourceRequestService
        {
            get;
            set;
        }

        [Dependency]
        public IWellCatalogService m_IWellCatalogService
        {
            get;
            set;
        }

        private Web.SystemLogin.Models.User m_CurrentUser
        {
            get
            {
                return (Web.SystemLogin.Models.User)Session["CurrentUser"];
            }
        }

        private readonly string m_WellType = string.Empty;

        #endregion


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!string.IsNullOrEmpty(Request.QueryString["wellType"]))
                {
                    //this.m_WellType = Request.QueryString["wellType"];
                    //this.BindData();
                }
                this.BindData();
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        #region 绑定数据

        private void BindData()
        {
            this.BindGV(ResourceRequestEnum.AgreeView, this.gv_ResoureRequestRecordListAgree, this.pagerAgree);
            this.BindGV(ResourceRequestEnum.DisAgreeView, this.gv_ResoureRequestRecordListDisAgree, this.AspNetPagerDisAgree);
            this.BindGV(ResourceRequestEnum.NewRequest, this.gv_ResoureRequestRecordListOrginal, this.AspNetPagerOrginal);
        }

        private void BindGV(ResourceRequestEnum requestEnum, EPGridView gv, AspNetPager pager)
        {
            DataTable oDataTable = this.m_IResourceRequestService.GetRequestList(requestEnum, this.m_CurrentUser.User_ID, this.m_WellType);
            if (oDataTable != null && oDataTable.Rows.Count > 0)
            {
                gv.PageSize = pager.PageSize;
                gv.DataSource = oDataTable;
                gv.DataBind();
            }
        }

        #endregion


        #region 绑定事件

        /// <summary>
        /// added by jiejiep
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_ResoureRequestRecordListDisAgree_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRow row = ((DataRowView)e.Row.DataItem).Row;
                try
                {
                    e.Row.Cells[4].Text = row["candownload"].ToString() == "1" ? "是" : "否";
                    e.Row.Cells[5].Text = row["canview"].ToString() == "1" ? "是" : "否";
                }
                catch (Exception)
                {

                }
            }
        }

        /// <summary>
        /// added by jiejiep
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gv_ResoureRequestRecordListAgree_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DataRow row = ((DataRowView)e.Row.DataItem).Row;
                try
                {
                    string attachmentID = string.Empty;
                    if (row["candownload"].ToString() == "1" || row["canview"].ToString() == "1")
                    {
                        attachmentID = this.GetAttachmentID(row["welldataid"].ToString());
                    }

                    if (row["candownload"].ToString() == "1")
                    {
                        ((HyperLink)e.Row.Cells[4].Controls[0]).Text = "是";
                        if (string.IsNullOrEmpty(attachmentID))
                        {
                            ((HyperLink)e.Row.Cells[4].Controls[0]).Enabled = false;
                        }
                        else
                        {
                            ((HyperLink)e.Row.Cells[4].Controls[0]).NavigateUrl = string.Format("../SingleWellManage/SingleWellAttachDownload.aspx?AttachmentID={0}&DBHelperString=SingleWell", attachmentID);
                        }
                    }
                    else
                    {
                        e.Row.Cells[4].Text = "否";
                    }

                    if (row["canview"].ToString() == "1")
                    {
                        ((HyperLink)e.Row.Cells[5].Controls[0]).Text = "是";
                        if (string.IsNullOrEmpty(attachmentID))
                        {
                            ((HyperLink)e.Row.Cells[5].Controls[0]).Enabled = false;
                        }
                        else
                        {
                            ((HyperLink)e.Row.Cells[5].Controls[0]).NavigateUrl = string.Format("../../WebDoc/WebDoc.aspx?AttachmentID={0}&AttachmentDBHelperString=SingleWell", attachmentID);
                        }
                    }
                    else
                    {
                        e.Row.Cells[5].Text = "否";
                    }
                }
                catch (Exception)
                {

                }
            }
        }

        private string GetAttachmentID(string p)
        {
            try
            {
                IAttachmentService oIAttachmentService = UContainer.Resolve<IAttachmentService>();
                oIAttachmentService.Initialize("SingleWell");
                List<AttachmentModel> modelList = oIAttachmentService.GetAttachmentModelByObjectID(p);
                if (modelList.Count > 0)
                {
                    return modelList[0].AttachmentID;
                }

                return string.Empty;
            }
            catch
            {
                return string.Empty;
            }
        }

        #endregion


        #region 分页代码

        protected void pagerAgree_PageChanging(object sender, PageChangingEventArgs e)
        {

        }

        protected void AspNetPagerOrginal_PageChanging(object sender, PageChangingEventArgs e)
        {

        }

        protected void AspNetPagerDisAgree_PageChanging(object sender, PageChangingEventArgs e)
        {

        }

        #endregion
    }
}